//
//  EFADgrAddTitleTCell.m
//  efairy
//
//  Created by XiangDongMBP on 2017/11/9.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFADgrAddTitleTCell.h"
#import "EFAModel.h"

@implementation EFADgrAddTitleTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    [self.textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)textFieldEditingChanged:(UITextField *)textField {
    // 文字长度限制
    NSInteger maxLength = 50;
    NSString *toBeString = textField.text;
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position || !selectedRange) {
        if (toBeString.length > maxLength) {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1) {
                textField.text = [toBeString substringToIndex:maxLength];
            } else {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    
    // 键盘不在联想状态才进行判断
    if (!selectedRange) {
        self.model.efairyhiddendanger_title = textField.text;
    }
}

- (void)configViewWithDangerPubModel:(EFADangerPubModel *)model {
    self.model = model;
    
    self.titleLabel.text = @"隐患标题";
    self.textField.text = model.efairyhiddendanger_title;
}

@end
